View Javadoc

1   /*   Copyright (C) 2004 Finalist IT Group
2    *
3    *   This file is part of JAG - the Java J2EE Application Generator
4    *
5    *   JAG is free software; you can redistribute it and/or modify
6    *   it under the terms of the GNU General Public License as published by
7    *   the Free Software Foundation; either version 2 of the License, or
8    *   (at your option) any later version.
9    *   JAG is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   *   GNU General Public License for more details.
13   *   You should have received a copy of the GNU General Public License
14   *   along with JAG; if not, write to the Free Software
15   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   */
17  
18  package com.finalist.tools.database;
19  
20  import java.util.*;
21  import java.text.*;
22  import java.sql.*;
23  import java.util.*;
24  import java.io.*;
25  import javax.naming.*;
26  import javax.sql.*;
27  import java.lang.reflect.*;
28  
29  import org.apache.commons.beanutils.BeanUtils;
30  
31  /***
32   * Helper class for StatementExecutor to delegate some work to
33   * This class is beta and rather undocumented.
34   *
35   * @author  P.S.D.Reitsma, Finalist IT Group
36   * @version 1.0
37   */
38  public class Helper {
39  
40     public static ArrayList convertToColumnList(HashMap template) {
41        Iterator it = template.keySet().iterator();
42        ArrayList columnList = new ArrayList();
43  
44        while (it.hasNext()) {
45           Object att = it.next();
46           columnList.add(new ColumnBean(createAttName(att),
47              createAttTypeAbr(att, template)));
48        }
49        return columnList;
50     }
51  
52  
53     publicng> static String generateBeanFromHashMap(String name, String packageName, HashMap template) {
54        returntrong> generateBeanFromColumnList(name, packageName, convertToColumnList(template));
55     }
56  
57  
58     publicng> static String generateBeanFromColumnList(String name, String packageName, ArrayList columnList) {
59  
60        StringBuffer newBeanDef = new StringBuffer();
61        newBeanDef.append("package " + packageName + ";\n");
62        newBeanDef.append("\n");
63        newBeanDef.append("/**\n");
64        newBeanDef.append(" * ValueObject class.\n");
65        SimpleDateFormat s = new SimpleDateFormat("dd-MM-yyyy");
66        String now = s.format(Calendar.getInstance().getTime());
67        newBeanDef.append(" * Generated by StatementExecutor on " + now + "\n");
68        newBeanDef.append(" */\n");
69        newBeanDef.append("public class " + name + " {\n");
70        newBeanDef.append("\n");
71  
72        for (int i = 0; i < columnList.size(); i++) {
73           ColumnBean column = (ColumnBean) columnList.get(i);
74           newBeanDef.append("  private " +
75              column.getJavaType() + " " +
76              createAttName(column.getName()) + ";\n");
77        }
78        newBeanDef.append("\n");
79  
80        for (int i = 0; i < columnList.size(); i++) {
81           ColumnBean column = (ColumnBean) columnList.get(i);
82           String attName = createAttName(column.getName());
83           String attNameInitCap = createAttNameInitCap(attName);
84  
85           //Object attObj = (Object)template.get(att);
86           String attTypeAbr = column.getJavaType();
87  
88           //generate the getter
89           newBeanDef.append("  /**\n");
90           newBeanDef.append("   * Getter method for property " + attName + "\n");
91           newBeanDef.append("   * @return " + attName + "\n");
92           newBeanDef.append("   */\n");
93           newBeanDef.append("  public " + attTypeAbr + " get" + attNameInitCap + "() {\n");
94           newBeanDef.append("    return this." + attName + ";\n");
95           newBeanDef.append("  }\n");
96           newBeanDef.append("\n");
97  
98           //generate the setter
99           newBeanDef.append("  /**\n");
100          newBeanDef.append("   * Setter method for property " + attName + "\n");
101          newBeanDef.append("   * @param pValue - input variable \n");
102          newBeanDef.append("   */\n");
103          newBeanDef.append("  public void set" + attNameInitCap + "(" + attTypeAbr + " pValue) {\n");
104          newBeanDef.append("    this." + attName + "= pValue;\n");
105          newBeanDef.append("  }\n");
106          newBeanDef.append("\n");
107       }
108       newBeanDef.append("   public String toString() {\n");
109       newBeanDef.append("       return \"\"\n");
110       for (int i = 0; i < columnList.size(); i++) {
111          ColumnBean column = (ColumnBean) columnList.get(i);
112          String attName = createAttName(column.getName());
113          String attNameInitCap = createAttNameInitCap(attName);
114          newBeanDef.append("       +\"" + attName + "=\"+get" + attNameInitCap + "()+\" \"\n");
115       }
116       newBeanDef.append(";\n   }\n");
117       newBeanDef.append("}\n");
118       return newBeanDef.toString();
119    }
120 
121 
122    public static String createAttName(Object att) {
123       String attName = ((String) att).toLowerCase();
124 
125       int i;
126       while ((i = attName.indexOf("_")) > -1) {
127          attName = attName.substring(0, i) + attName.substring(i + 1, i + 2).toUpperCase() + attName.substring(i + 2);
128       }
129       return attName;
130    }
131 
132 
133    public static String createAttNameInitCap(Object att) {
134       String attName = createAttName(att);
135       return attName.substring(0, 1).toUpperCase() + attName.substring(1);
136    }
137 
138 
139    public static String createAttTypeAbr(Object attObj, HashMap template) {
140       String attType;
141       if ((attObj != null) && ((Object) template.get(attObj) != null)) {
142          attType = ((Object) template.get(attObj)).getClass().getName();
143       }
144       else {
145          attType = "undetermined";
146       }
147       String attTypeAbr = attType;
148       if (attType.startsWith("java.lang.")) {
149          attTypeAbr = attType.substring(10);
150       }
151       if (attTypeAbr.equals("Byte")) attTypeAbr = "byte";
152       if (attTypeAbr.equals("Short")) attTypeAbr = "short";
153       if (attTypeAbr.equals("Integer")) attTypeAbr = "int";
154       if (attTypeAbr.equals("Long")) attTypeAbr = "long";
155       if (attTypeAbr.equals("Float")) attTypeAbr = "float";
156       if (attTypeAbr.equals("Double")) attTypeAbr = "double";
157       return attTypeAbr;
158    }
159 }